// 画面点位数据
let pointArr = []
// 当前点位的数据
let nowPoint = []
// 当前多边形中的点和线实体集合
let pointAndLineEntity = {
  pointEntityArr: [],
  lineEntityArr: [],
  demoLineEntityArr: [],
  polygonEntityArr: [],
}
// 当前选择的编辑点
let nowEditPoint = []
// 是否开始画多边形
let isDrawLine = false
// 是否形成面
let isDrawPolyGon = false
// 事件
const editPolyGonFunC = {
  // 删除实体
  // 删除鼠标移动生成的线
  delDemoEntity: (drawArr) => {
    drawArr.forEach((item) => {
      viewer.entities.remove(item)
    })
    drawArr=[]
  },
  // 画面
  // 画点
  drawPoint: (lonAndLat) => {
    const entityPoint = viewer.entities.add({
      position: Cesium.Cartesian3.fromDegrees(lonAndLat[0], lonAndLat[1], lonAndLat[2]),
      name: 'point_name',
      point: {
        color: Cesium.Color.fromCssColorString('#fc3d4a'),
        outlineColor: Cesium.Color.fromCssColorString('#fc3d4a'),
        pixelSize: 11,
        zIndex:3,
      },
    })
    return entityPoint
  },
  //多边形四周编辑点
  drawEditPoint: (cartesian) => {
    const entityPoint = viewer.entities.add({
      position: cartesian,
      name: 'polygon_point',
      point: {
        color: Cesium.Color.fromCssColorString('#fffff'),
        outlineColor: Cesium.Color.fromCssColorString('#0091ff'),
        outlineWidth: 2,
        pixelSize: 6,
        zIndex:3,
      },
    })
    return entityPoint
  },
  // 画线
  drawLine: (name, lineArr) => {
    const entityLine = viewer.entities.add({
      name: name,
      polyline: {
        positions: Cesium.Cartesian3.fromDegreesArrayHeights(lineArr),
        width: 3,
        material: Cesium.Color.fromCssColorString('#fcc31f'),
      },
    })
    return entityLine
  },
  // 画面
  drawPolyGon: (id,lonAndLat,color,zIndex) => {
    let lonLats = (data) => {
      let arr = []
      for (let d in data) {
        //判断经纬度范围
        if (data[d][0] >= -180 && data[d][0] <= 180 && data[d][1] >= -90 && data[d][1] <= 90) {
          arr.push(data[d][0], data[d][1])
        } else {
          console.log('经纬度数值不对：', data[d][0], data[d][1])
        }
      }
      return arr
    }
    const entityPolygon =viewer.entities.add({
      // position: Cesium.Cartesian3.fromDegrees(lonAndLat[0], lonAndLat[1], lonAndLat[2]),
      id:id,
      name: 'polyGon_name',
      polygon: {
        clampToGround: false, //开启贴地
        hierarchy: new Cesium.PolygonHierarchy(
          Cesium.Cartesian3.fromDegreesArray(lonLats(lonAndLat))
        ),
        // material: Cesium.Color.fromCssColorString('#4d90fe').withAlpha(0.4),
        material: Cesium.Color.fromCssColorString(color).withAlpha(0.4),
        zIndex: zIndex,
        // distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0, 300000)
      },
    })
    return entityPolygon;
  },
  // 经纬度转换
  getLonOrLat: (cartesian2) => {
    var cartesian = viewer.scene.globe.pick(viewer.camera.getPickRay(cartesian2), viewer.scene)
    // console.log('cartesian',cartesian);
    let cartorgraphic = Cesium.Cartographic.fromCartesian(cartesian)
    // console.log('ddd', cartorgraphic);
    let lon = Cesium.Math.toDegrees(cartorgraphic.longitude) // 经度
    let lat = Cesium.Math.toDegrees(cartorgraphic.latitude) // 纬度
    // console.log(`经度：${lon},纬度:${lat}`);
    return {
      longitude: lon,
      latitude: lat,
    }
  },

  // 重置
  klk: () => {
    editPolyGonFunC.delDemoEntity('point_name')
    isDrawLine = false
    isDrawPolyGon = false
    pointAndLineEntity = {
      pointEntityArr: [],
      lineEntityArr: [],
      demoLineEntityArr: [],
      polygonEntityArr: [],
    }
    nowEditPoint = []
  },
}
export default editPolyGonFunC
